# coding:utf-8
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from numpy import *
from matplotlib import pyplot as plt
from sklearn.linear_model import LinearRegression

x_data = np.array(
    [[100, 4, 4],
     [50, 1, 5],
     [100, 4, 3],
     [100, 3, 5],
     [50, 2, 6],
     [80, 2, 2],
     [75, 3, 1],
     [65, 4, 0],
     [90, 3, 0],
     [90, 2, 0]])
# 房屋价格（单位百万）
y_data = np.array([9.3, 4.8, 8.9, 6.5, 4.2, 6.2, 7.4, 6.0, 7.6, 6.1])
print(x_data)
print(y_data)

# 建立模型
model = LinearRegression()
# 开始训练
model.fit(x_data, y_data)

# 结果显示
# 斜率
print("coefficients: ", model.coef_)
w1 = model.coef_[0]
w2 = model.coef_[1]

# 截距
print("intercept: ", model.intercept_)
b = model.intercept_

# 测试
x_test = np.array([[90, 3, 0]])  # 只有变成数组后面才可以使用scatter,如果是x_test=[[90,3]],可以计算predict，但是没法画该点的散点图
predict = model.predict(x_test)
print("predict: ", predict)

# 模型评定：
from sklearn.metrics import accuracy_score  # 正确率
from sklearn.metrics import precision_score  # 精准率
from sklearn.metrics import recall_score  # 召回率
from sklearn.metrics import f1_score  # 调和平均值F1

y_test = y_data
y_hat = []
for i in range(len(y_data)):
    x_test = np.array([x_data[i]])  # 只有变成数组后面才可以使用scatter,如果是x_test=[[90,3]],可以计算predict，但是没法画该点的散点图
    predict = model.predict(x_test)
    print(type(predict))
    p = predict.tolist
    y_hat.append(p)

# 默认1类别为正例，可通过pos_label参数指定
print('正确率', accuracy_score(y_test, y_hat))
print('精准率', precision_score(y_test, y_hat))
print('召回率', recall_score(y_test, y_hat))
print('调和平均值F1', f1_score(y_test, y_hat))
